<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        ul{
            list-style: none;
            margin: 0;
            padding:0
        }
        li{
            display: flex;
            align-items: center;
        }
        li img{
            width: 150px;
            height: 80px;
        }
        span{
            position: absolute;
            width: 150px;
            height: 80px;
            display: block;
            transition:all 2s linear;
        }
        .cart{
            width: 150px;
            height: 50px;
            line-height: 50px;
            color:#fff;
            text-align: center;
            position: fixed;
            right: 20px;
            bottom:20px;
            background: palegreen;
        }
    </style>
</head>
<body>
    <div id="app">
        <ul>
            <li v-for="(list,index) in lists" :key="index" ref="listArry">
                <img :src="list.cover" alt="">
                <button @click="addCart(index)">加入购物车</button>
            </li>
        </ul>
        <transition @enter="enter" @after-enter="after">
            <span v-if="isShow"></span>
        </transition>
       <div class="cart" ref="cart">购物车</div>

    </div>
</body>
</html>
<script src="vue.js"></script>
<script> 
    let vm = new Vue({
        el:'#app',
        data:{
            isShow:false,
            currentIndex:-1,
            lists:[{
                            cover: 'http://www.javascriptpeixun.cn/files/course/2019/08-02/203853de0f7a753037.png',
                            id: 1,
                        },
                        {
                            cover: 'http://www.javascriptpeixun.cn/files/course/2019/07-30/202711f70eea987000.jpg',
                            id: 2,
                        },
                        {
                            cover: 'http://www.javascriptpeixun.cn/files/course/2019/09-04/211735f96b30549920.jpg',
                            id: 3,
                        }
                    ]
        },
        methods:{
            enter(el,done){//el当然操作的动画的元素  done动画完后执行的函数
                //确定点击元素li的位置
                //span的位置定位成跟li的位置一样
                //购物车的位置
                //求出span(图片)到购物车的移动距离
                let li = this.$refs.listArry[this.currentIndex] //点击的li
                let {x,y} = li.getBoundingClientRect(); //li距离窗口的位置
                el.style.left = x+"px";
                el.style.top = y+"px";
                el.style.background=`url(${this.lists[this.currentIndex].cover}) no-repeat`;
                el.style.backgroundSize="100% 100%";
                let {x:a,y:b} = this.$refs.cart.getBoundingClientRect();
                el.style.transform=`translate3d(${a-x}px,${b-y}px,0) scale(0)`;
                el.addEventListener('transitionend',done,false);
            },
            after(){
                this.isShow = false;
            },
            change() {
                this.isShow = !this.isShow;
            },
            addCart(index){
                this.isShow = true;
                this.currentIndex = index;
            }
        }
    })
</script>